package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;


	/**
	 * 根据用户ID查询用户信息
	 * @param userId
	 * @return
	 */
	@Operation(summary = "根据用户ID查询用户信息")
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
		return Result.ok(userInfoVo);
	}



	/**
	 * 获取每一页声音列表（提交声音ID集合）对应购买状态
	 *
	 * @param userId                       用户ID
	 * @param albumId                      专辑ID
	 * @param needCheckPayStateTrackIdList 需要检查购买状态的声音ID集合
	 * @return {声音ID:1(已购),声音ID:0(未购)}
	 */
	@GuiGuLogin  //对内微服务访问接口，上游系统调用是否只要携带token 下游系统就可以获取用户ID
	@Operation(summary = "获取每一页声音列表（提交声音ID集合）对应购买状态")
	@PostMapping("/userInfo/userIsPaidTrack/{albumId}")
	public Result<Map<Long, Integer>> userIsPaidTrack(
			@PathVariable("albumId") Long albumId,
			@RequestBody List<Long> needCheckPayStateTrackIdList
	) {
		Long userId = AuthContextHolder.getUserId();
		Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, needCheckPayStateTrackIdList);
		return Result.ok(map);
	}


	/**
	 * 判断当前用户是否购买过指定专辑
	 *
	 * @param albumId
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "判断当前用户是否购买过指定专辑")
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
		Long userId = AuthContextHolder.getUserId();
		Boolean isPaidAlbum = userInfoService.isPaidAlbum(userId, albumId);
		return Result.ok(isPaidAlbum);
	}


	/**
	 * 查询当前用户某个专辑下已购声音ID列表
	 * @param albumId
	 * @return
	 */
	@Operation(summary = "查询当前用户某个专辑下已购声音ID列表")
	@GuiGuLogin
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId) {
		Long userId = AuthContextHolder.getUserId();
		List<Long> paidTrackIdList = userInfoService.findUserPaidTrackList(userId, albumId);
		return Result.ok(paidTrackIdList);
	}

	/**
	 * 用户支付成功后，权益方法，虚拟物品（VIP会员、专辑、声音）发货
	 * 如果支付方式是微信，微信支付会异步回调，没有token造成接口无法调用，故不加@GuiGuLogin注解
	 * @param userPaidRecordVo
	 * @return
	 */
	@Operation(summary = "用户支付成功后，权益方法，虚拟物品（VIP会员、专辑、声音）发货")
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();
	}


	/**
	 * 更新VIP状态：处理过期会员
	 * @return
	 */
	@Operation(summary = "更新VIP状态：处理过期会员")
	@GetMapping("/updateVipExpireStatus")
	public Result updateVipExpireStatus(){
		userInfoService.updateVipExpireStatus(new Date());
		return Result.ok();
	}
}

